﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Blokus.Controls;

namespace Blokus.Scenes
{
    public class SceneMenu : Scene
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private SpriteFont menuFont;
        public static bool useAI = false;
        private readonly int menuNewGame;
        private readonly int menuDesignerMenu;
        private readonly int menuHighScores;
        private readonly int menuControls;
        private readonly int menuCredits;
        private readonly int menuExit;

        private List<string> menuItems = new List<string>();
        private int currentMenuIndex = 0;
        private KeyboardState keyPrevState;
        private GamePadState gpPrevState;
        private Texture2D texBG;
        private float aspectRatio;
        private RenderTarget2D backgroundRenderTarget;

        private Texture2D rectn;
        private TextBox btnNewGame;
        private TextBox btnDesigner;
        private Blokus game;

        public SceneMenu(Blokus game)
        {
            this.game = game;
            
            menuNewGame = menuItems.Count;
            menuItems.Add("New Game");
            menuDesignerMenu = menuItems.Count;
            menuItems.Add("Designer Menu");
            menuHighScores = menuItems.Count;
            menuItems.Add("High Scores");
            menuControls = menuItems.Count;
            menuItems.Add("Controls");
            menuCredits = menuItems.Count;
            menuItems.Add("Credits");
            menuExit = menuItems.Count;
            menuItems.Add("Quit");
        }

        public override void LoadContent(ContentManager content, GraphicsDeviceManager graphics)
        {
            this.graphics = graphics;
            //load font
            menuFont = content.Load<SpriteFont>("gamefont");
            backgroundRenderTarget = new RenderTarget2D(graphics.GraphicsDevice, 1280, 800);
            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

            texBG = content.Load<Texture2D>("Textures\\background");
            rectn = content.Load<Texture2D>("Textures/rectangle");
            aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;

            btnNewGame = new TextBox("New Game", false, new Rectangle(350, 350, 125, 75));
            btnNewGame.padding = 100;
            btnNewGame.backgroundTexture = rectn;
            btnNewGame.spriteBatch = spriteBatch;
            btnNewGame.GameFont = menuFont;
            btnNewGame.colorOnMouseOver = true;

            btnDesigner = new TextBox("Designer", false, new Rectangle(700, 350, 125, 75));
            btnDesigner.padding = 100;
            btnDesigner.backgroundTexture = rectn;
            btnDesigner.spriteBatch = spriteBatch;
            btnDesigner.GameFont = menuFont;
            btnDesigner.colorOnMouseOver = true;
        }

        public int milliDemoCounter = 0;
        public override Blokus.GameState Update(GameTime gameTime)
        {
            game.IsMouseVisible = true;
            if (btnNewGame.Update(gameTime, Mouse.GetState()))
            {
                btnNewGame.hasFocus = false;
                return Blokus.GameState.NEWGAME;
            }
            else if (btnDesigner.Update(gameTime, Mouse.GetState()))
            {
                btnDesigner.hasFocus = false;
                return Blokus.GameState.DESIGNER_MENU;
            }
            //number of menu items minus one
            //int menuMax = 5;
            ////get keys
            //GamePadState currGPState = GamePad.GetState(PlayerIndex.One);
            //bool kUp = Keyboard.GetState().IsKeyDown(Keys.Up) && !keyPrevState.IsKeyDown(Keys.Up)
            //    || currGPState.IsButtonDown(Buttons.DPadUp) && !gpPrevState.IsButtonDown(Buttons.DPadUp);
            //bool kDown = Keyboard.GetState().IsKeyDown(Keys.Down) && !keyPrevState.IsKeyDown(Keys.Down)
            //    || currGPState.IsButtonDown(Buttons.DPadDown) && !gpPrevState.IsButtonDown(Buttons.DPadDown);
            //bool kLeft = Keyboard.GetState().IsKeyDown(Keys.Left) && !keyPrevState.IsKeyDown(Keys.Left)
            //    || currGPState.IsButtonDown(Buttons.DPadLeft) && !gpPrevState.IsButtonDown(Buttons.DPadLeft);
            //bool kRight = Keyboard.GetState().IsKeyDown(Keys.Right) && !keyPrevState.IsKeyDown(Keys.Right)
            //    || currGPState.IsButtonDown(Buttons.DPadRight) && !gpPrevState.IsButtonDown(Buttons.DPadRight);
            //bool kEnter = Keyboard.GetState().IsKeyDown(Keys.Enter) && !keyPrevState.IsKeyDown(Keys.Enter)
            //    || currGPState.IsButtonDown(Buttons.A) && !gpPrevState.IsButtonDown(Buttons.A);
            //milliDemoCounter += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
            //if (kUp || kDown || kLeft || kRight || kEnter)
            //    milliDemoCounter = 0;


            //check for menu movement

            //if (kUp)
            //{
            //    currentMenuIndex--;
            //    if (currentMenuIndex < 0)
            //        currentMenuIndex = menuMax;
            //}
            //if (kDown)
            //{
            //    currentMenuIndex++;
            //    if (currentMenuIndex > menuMax)
            //        currentMenuIndex = 0;
            //}

            ////check for menu item selected
            //if (kEnter)
            //{
            //    if (currentMenuIndex == menuNewGame)
            //    {
            //        useAI = false;
            //        return Blokus.GameState.NEWGAME;
            //    }
            //    else if (currentMenuIndex == menuDesignerMenu)
            //    {
            //        return Blokus.GameState.DESIGNER_MENU;
            //    }
            //    else if (currentMenuIndex == menuHighScores)
            //    {
            //        return Blokus.GameState.HIGHSCORES;
            //    }
            //    else if (currentMenuIndex == menuControls)
            //    {
            //        return Blokus.GameState.CONTROLS;
            //    }
            //    else if (currentMenuIndex == menuCredits)
            //    {
            //        return Blokus.GameState.CREDITS;
            //    }
            //    else if (currentMenuIndex == menuExit)
            //    {
            //        return Blokus.GameState.EXIT;
            //    }
            //}
            //if (milliDemoCounter > 30 * 1000)
            //{
            //    useAI = true;
            //    return Blokus.GameState.PLAYING;
            //}

            //reset previous keyboard state
            keyPrevState = Keyboard.GetState();
            gpPrevState = GamePad.GetState(PlayerIndex.One);

            return Blokus.GameState.MENU;
        }

        public override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(colorMiddle);
            spriteBatch.Begin();
            
            //menu items
            //spriteBatch.Draw(texBG, new Rectangle(0, 0, 1280, 800), Color.White);
            spriteBatch.DrawString(menuFont, "BLOKUS++", new Vector2(550, 175), colorDark);
            //for (int ct = 0; ct < menuItems.Count; ct++)
            //{
            //    Color clr = colorLight;
            //    Random random = new Random();
            //    if (currentMenuIndex == ct)
            //        clr = colorDark;
            //    string menuStr = menuItems[ct];
            //    spriteBatch.DrawString(menuFont, menuStr, new Vector2(100, 200 + ct * 50), clr);
            //} 
            btnNewGame.Draw();
            btnDesigner.Draw();
            spriteBatch.End();
        }

        public override void ResetPrevKeys()
        {
            keyPrevState = Keyboard.GetState();
            gpPrevState = GamePad.GetState(PlayerIndex.One);
            SceneHighscores.newHighscore = -1;
            milliDemoCounter = 0;
        }

    }
}
